package dao;

import bean.Admin;
import bean.PageBean;
import net.sf.json.JSONObject;
import util.JdbcUtils;
import util.StringUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 管理员用户的Dao
 *
 * @author gloria
 */
public class AdminDao {
    /**
     * 管理员用户登录
     *
     * @param admin the admin
     * @return admin
     * @throws Exception the exception
     */
    public Admin login(Admin admin) throws Exception {
        String sql = "select * from admin where userName=? and password=?";
        List<Object> para = new ArrayList<>();
        para.add(admin.getUserName());
        para.add(admin.getPassword());
        List<Object> list = JdbcUtils.queryForArrObject(sql, para, Admin.class);
        return list.size() != 1 ? null : (Admin) list.get(0);
    }

    /**
     * 管理员用户一览
     *
     * @param pageBean the page bean
     * @param admin    the admin
     * @return list
     * @throws Exception the exception
     */
    public List<Object> adminList(PageBean pageBean, Admin admin) throws Exception {
        StringBuilder sb = new StringBuilder("select * from admin u ");
        List<Object> para = new ArrayList<>();
        if (StringUtil.isNotEmpty(admin.getAdminId())) {
            sb.append(" and u.adminId =?");
            para.add(admin.getAdminId());
        }
        if (StringUtil.isNotEmpty(admin.getUserName())) {
            sb.append(" and u.username like ?");
            para.add("%" + admin.getUserName() + "%");
        }
        if (StringUtil.isNotEmpty(admin.getPassword())) {
            sb.append(" and u.password =?");
            para.add(admin.getPassword());
        }
        if (StringUtil.isNotEmpty(admin.getName())) {
            sb.append(" and u.name like ?");
            para.add("%" + admin.getName() + "%");
        }
        if (StringUtil.isNotEmpty(admin.getAdminRoleId())) {
            sb.append(" and u.adminRoleId =?");
            para.add(admin.getAdminRoleId());
        }
        if (pageBean != null) {
            sb.append(" limit ").append(pageBean.getStart()).append(",").append(pageBean.getRows());
        }
        String sql = sb.toString().replaceFirst("and", "where");
        List<Object> list = JdbcUtils.queryForArrObject(sql, para, Admin.class);
        return list.size() == 0 ? Collections.emptyList() : list;
    }

    /**
     * 获得某个管理员的权限
     *
     * @param id the id
     * @return the privileges
     */
    public int getPrivileges(String id) {
        StringBuilder sb = new StringBuilder("select adminRoleId from admin u ");
        List<Object> para = new ArrayList<>();
        if (StringUtil.isNotEmpty(id)) {
            sb.append(" and u.adminId =?");
            para.add(id);
        }
        String sql = sb.toString().replaceFirst("and", "where");
        List<Map<String, String>> result = JdbcUtils.queryForMap(sql, para);
        return result.isEmpty() ? 0 : Integer.parseInt(result.get(0).get("adminRoleId"));
    }

    /**
     * 管理员用户删除
     *
     * @param delIds the del ids
     * @return int
     * @throws Exception the exception
     */
    public int adminDelete(String delIds) throws Exception {
        String sql = "delete from admin where adminId in(?)";
        List<Object> para = new ArrayList<>();
        para.add(delIds);
        return JdbcUtils.update(sql, para);
    }

    /**
     * 管理员用户添加
     *
     * @param admin the admin
     * @return boolean
     * @throws Exception the exception
     */
    public boolean adminAdd(Admin admin) throws Exception {
        StringBuilder sb = new StringBuilder("insert into admin values(");
        List<Object> para = new ArrayList<>();
        if (null != admin.getAdminId() && StringUtil.isNotEmpty(admin.getAdminId())) {
            sb.append(" ,?");
            para.add(admin.getAdminId());
        } else {
            sb.append(" ,null");
        }
        if (null != admin.getUserName() && StringUtil.isNotEmpty(admin.getUserName())) {
            sb.append(" ,?");
            para.add(admin.getUserName());
        } else {
            sb.append(" ,null");
        }
        if (null != admin.getPassword() && StringUtil.isNotEmpty(admin.getPassword())) {
            sb.append(" ,?");
            para.add(admin.getPassword());
        } else {
            sb.append(" ,null");
        }
        if (null != admin.getName() && StringUtil.isNotEmpty(admin.getName())) {
            sb.append(" ,?");
            para.add(admin.getName());
        } else {
            sb.append(" ,null");
        }
        if (null != admin.getAdminRoleId() && StringUtil.isNotEmpty(admin.getAdminRoleId())) {
            sb.append(" ,?");
            para.add(admin.getAdminRoleId());
        } else {
            sb.append(" ,null");
        }

        sb.append(")");
        String sql = sb.toString().replaceFirst(",", " ");
        return 1 == JdbcUtils.update(sql, para);
    }

    /**
     * 管理员用户修改
     *
     * @param admin the admin
     * @return boolean
     * @throws Exception the exception
     */
    public boolean adminModify(Admin admin) throws Exception {
        StringBuilder sb = new StringBuilder("update admin u ");
        List<Object> para = new ArrayList<>();
        if (StringUtil.isNotEmpty(admin.getUserName())) {
            sb.append(" , u.userName=?");
            para.add(admin.getUserName());
        }
        if (StringUtil.isNotEmpty(admin.getPassword())) {
            sb.append(" , u.password =?");
            para.add(admin.getPassword());
        }
        if (StringUtil.isNotEmpty(admin.getName())) {
            sb.append(" , u.name =?");
            para.add(admin.getName());
        }
        if (StringUtil.isNotEmpty(admin.getAdminRoleId())) {
            sb.append(" , u.adminRoleId=?");
            para.add(admin.getAdminRoleId());
        }
        sb.append(" where u.adminId=?");
        para.add(admin.getAdminId());
        String sql = sb.toString().replaceFirst(",", "set");
        int result = JdbcUtils.update(sql, para);

        return result == 1;
    }

    /**
     * Admin count int.
     *
     * @param admin the admin
     * @return the int
     * @throws Exception the exception
     */
    public int adminCount(Admin admin) throws Exception {
        StringBuilder sb = new StringBuilder("select count(*) as total from admin u");

        List<Object> para = new ArrayList<>();
        if (StringUtil.isNotEmpty(admin.getAdminId())) {
            sb.append(" and u.adminId =? ");
            para.add(admin.getAdminId());
        }
        if (StringUtil.isNotEmpty(admin.getUserName())) {
            sb.append(" and u.userName like ?");
            para.add("%" + admin.getUserName() + "%");
        }
        if (StringUtil.isNotEmpty(admin.getPassword())) {
            sb.append(" and u.password =?");
            para.add(admin.getPassword());
        }
        if (StringUtil.isNotEmpty(admin.getName())) {
            sb.append(" and u.name like ?");
            para.add("%" + admin.getName() + "%");
        }
        if (StringUtil.isNotEmpty(admin.getAdminRoleId())) {
            sb.append(" and u.adminRoleId=?");
            para.add(admin.getAdminRoleId());
        }
        String sql = sb.toString().replaceFirst("and", "where");
        List<Map<String, String>> result = JdbcUtils.queryForMap(sql, para);
        return result.isEmpty() ? 0 : Integer.parseInt(result.get(0).get("total"));
    }

    /**
     * Have message json object.
     *
     * @param admin the admin
     * @return the json object
     */
    public JSONObject haveMessage(Admin admin) {
        JSONObject result = new JSONObject();

        String sql = "select count(*) n from userInfo where userId is not null";
        List<Map<String, String>> map = JdbcUtils.queryForMap(sql, null);
        if ("0".equals(map.get(0).get("n"))) {
            result.put("user", false);
        } else {
            result.put("user", true);
        }
        sql = "select count(*) n from activity where status < 2";
        map = JdbcUtils.queryForMap(sql, null);
        if ("0".equals(map.get(0).get("n"))) {
            result.put("activities", false);
        } else {
            result.put("activities", true);
        }
        sql = "select count(*) n from advise where status < 1";
        map = JdbcUtils.queryForMap(sql, null);
        if ("0".equals(map.get(0).get("n"))) {
            result.put("feedback", false);
        } else {
            result.put("feedback", true);
        }
        return result;
    }

    /**
     * 编号为adminId的管理员是否有type类型的权限
     *
     * @param adminId the admin id
     * @param type    the type
     * @return boolean
     */
    public boolean havePermission(String adminId, String type) {
        String sql = "select " + type + " from adminRole, admin where admin.adminRoleId=adminRole.adminRoleId and admin.adminId=?";
        List<Object> para = new ArrayList<>();
        para.add(adminId);
        List<Map<String, String>> resultList = JdbcUtils.queryForMap(sql, para);
        if (null == resultList || resultList.size() == 0) {
            return false;
        } else {
            String result = resultList.get(0).get(type);
            return null != result && "1".equals(result);
        }
    }

    /**
     * 查询所有管理员（分页）
     * 勿删！
     *
     * @param pageBean 分页对象
     * @return 管理员集合 list
     * @since 2017年11月4日 00点57分
     */
    public List<Object> adminList2(PageBean pageBean) {
        StringBuilder sb = new StringBuilder("select * from admin");
        if (pageBean != null) {
            sb.append(" limit ").append(pageBean.getStart()).append(",").append(pageBean.getRows());
        }
        return JdbcUtils.queryForArrObject(sb.toString(), null, Admin.class);
    }
}
